package com.felix.ocoupon.advice;

import com.felix.ocoupon.annotation.IgnoreResponseAdvice;
import com.felix.ocoupon.vo.CommonResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author hsufelix
 * @email hsufelix@yeah.net
 * @Date 2020/11/11
 */
@RestControllerAdvice
public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> {
    /**
     * Whether this component supports the given controller method return type
     * and the selected {@code HttpMessageConverter} type.
     *
     * @param returnType    the return type
     * @param converterType the selected converter type
     * @return {@code true} if {@link #beforeBodyWrite} should be invoked;
     * {@code false} otherwise
     * <h2>判断是否需要对响应进行处理</h2>
     */
    @Override
    public boolean supports(MethodParameter returnType,
                            Class<? extends HttpMessageConverter<?>> converterType) {
        // 如果当前方法所在的类标识了@IgnoreResponseAdvice注解，就不需要使用统一结果处理
        if (returnType.getDeclaringClass().isAnnotationPresent(IgnoreResponseAdvice.class)) {
            return false;
        }
        // 如果当前方法标识了@IgnoreResponseAdvice注解，就不需要使用统一结果处理
        if (returnType.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)) {
            return false;
        }
        // 对响应进行处理，执行beforeBodyWrite方法
        return true;
    }

    /**
     * <h2>调用后，返回前对响应进行处理</h2>
     * Invoked after an {@code HttpMessageConverter} is selected and just before
     * its write method is invoked.
     *
     * @param obj                  the body to be written
     * @param methodParameter            the return type of the controller method
     * @param mediaType   the content type selected through content negotiation
     * @param selectedConverterType the converter type selected to write to the response
     * @param serverHttpRequest               the current request
     * @param serverHttpResponse              the current response
     * @return the body that was passed in or a modified (possibly new) instance
     */
    @Override
    public Object beforeBodyWrite(Object obj,
                                  MethodParameter methodParameter,
                                  MediaType mediaType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                  ServerHttpRequest serverHttpRequest,
                                  ServerHttpResponse serverHttpResponse) {
        // 定义
        CommonResponse<Object> response = new CommonResponse<>(0,"");
        // 如果 obj 参数是null，response不需要设置data
        if (null == obj) {
            return response;
        } else if (obj instanceof CommonResponse) {
            response = (CommonResponse<Object>) obj;
        } else {
            // 只输出data部分
            response.setData(obj);
        }
        return response;
    }
}
